<template>
  <div>
    <!-- 列表页面 -->
    <div class="container" v-if="!showEdit">
      <div class="header">
        <div class="title">列表</div>
      </div>
      <!-- 表格 -->
      <el-table :data="maintenances" v-loading="loading">
        <el-table-column type="index" :index="indexMethod" label="序号" width="60"></el-table-column>

<!--        <el-table-column prop="accept_id" label="受理表ID"></el-table-column>-->
        <el-table-column prop="work_number" label="工单序号"></el-table-column>
        <el-table-column prop="order_number" label="受理序号"></el-table-column>
        <el-table-column prop="order_time" :formatter="formatDateC" label="受理时间" width="100"></el-table-column>
        <el-table-column prop="username" label="用户名"></el-table-column>
        <el-table-column prop="phone" label="联系电话"></el-table-column>
        <el-table-column prop="type" label="终端类型"></el-table-column>
        <el-table-column prop="model" label="型号颜色"></el-table-column>
        <el-table-column prop="nature" label="维修性质"></el-table-column>
        <el-table-column prop="local_number" label="本机号码"></el-table-column>
        <el-table-column prop="serial_no" label="机身号码"></el-table-column>
<!--        <el-table-column prop="file" label="附件"></el-table-column>-->
        <el-table-column prop="content" label="修理内容"></el-table-column>
        <el-table-column prop="re_accessories" label="更换配件"></el-table-column>
        <el-table-column prop="accessory_amount" label="配件费"></el-table-column>
        <el-table-column prop="work_amount" label="工时费"></el-table-column>
        <el-table-column prop="total_amount" label="合计"></el-table-column>
        <el-table-column label="所修项目保用期">
          <template #default="scope">
            <!-- 这里可以根据数据特殊显示，例如标签、颜色等 -->
            <p>{{ scope.row.shelf_start_string + '至' + scope.row.shelf_end_string  }}</p>
          </template>
        </el-table-column>

        <el-table-column label="操作" fixed="right" width="405">
          <template #default="scope">
            <el-button plain size="small" type="primary" @click="handleEdit(scope.row.id)">编辑</el-button>
            <el-button
              plain
              size="small"
              type="danger"
              @click="handleDelete(scope.row.id)"
              v-permission="{ permission: '删除', type: 'disabled' }"
              >删除</el-button
            >
            <el-button plain size="small" type="primary" @click="exportDataWord(scope.row.id)">导出Word</el-button>
            <el-button plain size="small" type="primary" @click="exportData(scope.row.id)">导出excel</el-button>
            <el-button plain size="small" type="primary" @click="exportDataPdf(scope.row.id)">导出Pdf</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑页面 -->
    <maintenance-modify v-else @editClose="editClose" :editMaintenanceId="editMaintenanceId"></maintenance-modify>
  </div>
</template>

<script>
import { onMounted, ref } from 'vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import maintenanceModel from '@/model/maintenance'
import acceptModel from '@/model/accept'
import MaintenanceModify from './maintenance'

export default {
  components: {
    MaintenanceModify,
  },
  setup() {
    const maintenances = ref([])
    const editMaintenanceId = ref(1)
    const loading = ref(false)
    const showEdit = ref(false)

    onMounted(() => {
      getMaintenances()
    })

    const getMaintenances = async () => {
      try {
        loading.value = true
        maintenances.value = await maintenanceModel.getMaintenances()
        loading.value = false
      } catch (error) {
        loading.value = false
        if (error.code === 10020) {
          maintenances.value = []
        }
      }
    }

    const handleEdit = id => {
      showEdit.value = true
      editMaintenanceId.value = id
    }

    const handleDelete = id => {
      ElMessageBox.confirm('此操作将永久删除该, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const res = await maintenanceModel.deleteMaintenance(id)
        if (res.code < window.MAX_SUCCESS_CODE) {
          getMaintenances()
          ElMessage.success(`${res.message}`)
        }
      })
    }

    const editClose = () => {
      showEdit.value = false
      getMaintenances()
    }

    // script->method
    // 时间格式化
    const formatDateC = (row, column) => {
      let datetime = row[column.property]
      if (datetime) {
        datetime = new Date(datetime)
        const y = `${datetime.getFullYear()}-`
        const mon = `${datetime.getMonth() + 1}-`
        const d = datetime.getDate()
        return y + mon + d
      }
      return ''
    }

    const exportDataWord = async id => {
      ElMessageBox.confirm('要导出记录的维修单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const params = {
          id,
        }
        await maintenanceModel.exportAllWord(null, params).then(res => {
          downloadFile(res, '手机维修单', 'doc', true)
        })
      })
    }

    const exportDataPdf = async id => {
      ElMessageBox.confirm('要导出记录的维修单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const params = {
          id,
        }
        await maintenanceModel.exportAllPdf(null, params).then(res => {
          downloadFile(res, '手机维修单', 'pdf', true)
        })
      })
    }

    const exportData = async id => {
      ElMessageBox.confirm('要导出记录的维修单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        const params = {
          id,
        }
        await maintenanceModel.exportAllExcel(null, params).then(res => {
          downloadFile(res, '手机维修单', 'xls', true)
        })
      })
    }

    const downloadFile = (obj, name, suffix, isAppendDate) => {
      const url = window.URL.createObjectURL(new Blob([obj]))
      const link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      const date = new Date()
      const mon = date.getMonth() + 1
      const day = date.getDate()
      const curDateStr = `${date.getFullYear()}${mon < 10 ? `0${mon}` : mon}${day < 10 ? `0${day}` : day}`

      const fileName = `${name + (isAppendDate ? `-${curDateStr}` : '')}.${suffix}`
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }

    const indexMethod = index => index + 1

    return {
      maintenances,
      loading,
      showEdit,
      editClose,
      handleEdit,
      editMaintenanceId,
      indexMethod,
      handleDelete,
      formatDateC,
      exportData,
      exportDataWord,
      exportDataPdf
    }
  },
}
</script>

<style lang="scss" scoped>
.container {
  padding: 0 30px;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .title {
      height: 59px;
      line-height: 59px;
      color: $parent-title-color;
      font-size: 16px;
      font-weight: 500;
    }
  }

  .pagination {
    display: flex;
    justify-content: flex-end;
    margin: 20px;
  }
}
</style>
